package com.xique.door.wechat.service;

import com.xique.door.bean.domain.WxAutoReply;
import com.xique.door.service.wechat.IWechatGzhNotifyService;
import com.xique.door.service.wechat.IWxAutoReplyService;
import com.xique.door.wechat.domain.TextMessage;
import com.xique.door.wechat.util.WechatMessageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @program: xq-cloud
 * @ClassName SubscribeReplyService
 * @description:
 * @author: HdongSun
 * @create: 2022-10-26 16:20
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class SubscribeReplyService {

    private static final String FROM_USER_NAME = "FromUserName";
    private static final String TO_USER_NAME = "ToUserName";
    private static final String CONTENT = "Content";

    private final IWechatGzhNotifyService wechatGzhNotifyService;

    private final IWxAutoReplyService wxAutoReplyService;

    /**
     * 自动回复文本内容
     *
     * @param requestMap
     * @return
     */
    public String reply(Map<String, String> requestMap) {
        String wechatId = requestMap.get(FROM_USER_NAME);
        String gongzhonghaoId = requestMap.get(TO_USER_NAME);

        TextMessage textMessage = WechatMessageUtil.getDefaultTextMessage(wechatId, gongzhonghaoId);

        String content = requestMap.get(CONTENT);
        if (content == null) {
            List<WxAutoReply> defaultList = wxAutoReplyService.selectListByKeyword("关注");
            WxAutoReply reply = defaultList.get(0);
            textMessage.setContent(reply.getContent());
        }

        // TODO 处理订阅后时间 获取用户基础信息 记录到数据库
        wechatGzhNotifyService.delaySubscribeEvent(wechatId);

        return WechatMessageUtil.messageToXml(textMessage);
    }
}
